package com.anolesoft.yr.mbus.sup.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.part.dto.PartModelDTO;
import com.anolesoft.yr.mbus.part.vo.PartModelVO;
import com.anolesoft.yr.mbus.sup.dto.SupBaseDTO;
import com.anolesoft.yr.mbus.sup.dto.SupBasePartDTO;
import com.anolesoft.yr.mbus.sup.service.SupBasePartService;
import com.anolesoft.yr.mbus.sup.vo.SupBasePartVO;
import com.anolesoft.yr.mbus.sup.vo.SupBaseVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/24 14:16
 */
@RestController
@RequestMapping("/api/sup/supbasepart")
public class SupBasePartController {
    @Autowired
    private SupBasePartService supBasePartService;

    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(SupBasePartVO supBasePartVO) {
        SupBasePartDTO supBasePartDTO = vo2dto(supBasePartVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, supBasePartService.create(supBasePartDTO));
    }

    private SupBasePartDTO vo2dto(SupBasePartVO supBasePartVO,boolean newId) {
        return SupBasePartDTO.builder()
                .factoryCode(supBasePartVO.getFactoryCode())
                .modifyUserUid(SecurityUtils.getUserId())
                .remark(supBasePartVO.getRemark())
                .objuid(newId ? CommonUtils.fastUUID() : supBasePartVO.getObjuid())
                .createUserUid(SecurityUtils.getUserId())
                .partModelUid(supBasePartVO.getPartModelUid())
                .supBaseUid(supBasePartVO.getSupBaseUid())
                .build();
    }

    @PutMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(SupBasePartVO supBasePartVO) {
        SupBasePartDTO supBasePartDTO = vo2dto(supBasePartVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, supBasePartService.modify(supBasePartDTO));
    }

    @DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, supBasePartService.remove(id));
    }

    @DeleteMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult removeBatch(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, supBasePartService.removeBatch(ids));
    }

    @GetMapping(value = "/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<SupBasePartDTO> supBasePartDTO = supBasePartService.selectOneById(id);
        if (supBasePartDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.supBasePartDTO2VO(supBasePartDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }
    }

    @GetMapping(value = "/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageList(SupBasePartVO supBasePartVO) {
        SupBasePartDTO supBasePartDTO = vo2dto(supBasePartVO, false);

        Integer limit = supBasePartVO.getRows();
        Integer curr = supBasePartVO.getPage();
        PageData<List<SupBasePartDTO>> pageData = supBasePartService.selectPageList(supBasePartDTO, limit, curr);
        List<SupBasePartVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.supBasePartDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<SupBasePartVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping(value = "/list/condition/union/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageConditonList(@RequestBody SupBasePartVO supBasePartVO) {
        SupBasePartDTO supBasePartDTO = vo2dto(supBasePartVO, false);
        SupBaseVO supBase = supBasePartVO.getSupBase();
        PartModelVO partModelVO = supBasePartVO.getPartModel();
        if (supBasePartVO != null) {
            if (supBase != null) {
                SupBaseDTO sup = SupBaseDTO.builder()
                        .supCode(supBase.getSupCode())
                        .supName(supBase.getSupName())
                        .supOtherName(supBase.getSupOtherName())
                        .build();
                supBasePartDTO.setSupBase(sup);
            }
            if (partModelVO != null) {
                PartModelDTO model = PartModelDTO.builder()
                        .partTypeUid(partModelVO.getPartTypeUid())
                        .partBrand(partModelVO.getPartBrand())
                        .partName(partModelVO.getPartName())
                        .partModel(partModelVO.getPartModel())
                        .partCode(partModelVO.getPartCode())
                        .partSpecification(partModelVO.getPartSpecification())
                        .build();
                supBasePartDTO.setPartModel(model);
            }
        }

        Integer limit = supBasePartVO.getRows();
        Integer curr = supBasePartVO.getPage();
        PageData<List<SupBasePartDTO>> pageData = supBasePartService.selectPageConditionList(supBasePartDTO, limit, curr);
        List<SupBasePartVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.supBasePartDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<SupBasePartVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }
}
